package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<CategoryDTO> queryByParentId(Long pid) {
        Category record = new Category();
        record.setParentId(pid);

        List<Category> select = categoryMapper.select(record);

        if (CollectionUtils.isEmpty(select)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOND);
        }

        //把当前集合转成新的集合，并且集合中对象的类型为CategoryDTO
        return BeanHelper.copyWithCollection(select,CategoryDTO.class);
    }

    public List<CategoryDTO> queryByBrandId(Long brandId) {
        List<Category> categories = this.categoryMapper.queryByBrandId(brandId);

       if (CollectionUtils.isEmpty(categories)){
           throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
       }

        return  BeanHelper.copyWithCollection(categories,CategoryDTO.class);
    }

    //根据分类的id查询对应的分类信息
    public List<CategoryDTO> queryCategoryByIds(List<Long> categoryIds) {

        //新增一个接口，新增了一次性传入一个集合参数查询
        List<Category> categories = this.categoryMapper.selectByIdList(categoryIds);

        if (CollectionUtils.isEmpty(categories)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(categories,CategoryDTO.class);
    }
}
